<?php

include_once APPPATH . '/helpers/session_helper.php';
include_once APPPATH . '/helpers/fly_search_helper.php';
include_once APPPATH . 'libraries/travelport/Travelport.php';
include_once APPPATH . '/models/constants/flight_constants.php';
include_once APPPATH . '/models/fly_search/filtered_combined_air_price_solution.php';

class Fly_search extends CI_Model {

    public function search($search_criteria = null) {
        if ($search_criteria == null) {
            return null;
        }
        $session = new Session();
        $session->set(Fly_Constant::SESSION_SEARCH_CRITERIA_PARAMETER, $search_criteria);
        return $this->sendMessageAPIs($search_criteria);
    }

    public function sendMessageAPIs($search_criteria) {
       // $result_xml = file_get_contents("low_search_res2_1.xml");
        $travelportapi_instance = Travelport::getInstance();
        $result_xml = $travelportapi_instance->searchFlights($search_criteria);
       
        if ((int)$search_criteria->flydirection == 2) {
            file_put_contents("low_search_res2.xml", $result_xml);
        } else {
            //file_put_contents("low_search_res.xml", $result_xml);
        }
       
        //a
        $travelport_low_search_response = $travelportapi_instance->parseLowFareSearchTravelPortApi($result_xml, $search_criteria);
        $min_price_solution = $travelport_low_search_response->air_price_solutions[0];
        $max_price_solution = $travelport_low_search_response->air_price_solutions[count($travelport_low_search_response->air_price_solutions) - 1];
        $travelport_low_search_response->min_price = $min_price_solution->apprixomate_total_price_amount;
        $travelport_low_search_response->max_price = $max_price_solution->apprixomate_total_price_amount;
         //$travelport_low_search_response->min-price = ($travelport_low_search_response->air_price_solutions[0])->apprixomate_total_price_amount;
        $travelport_low_search_response->best_price_table_data = Fly_seach_helper::createFlightSummayTableData($travelport_low_search_response);
        $combined_air_solutions_array = $travelportapi_instance->combineAirPriceSolutions($travelport_low_search_response);
        $session = new Session(720);
        $session->set("travelport_response", $travelport_low_search_response);
        $session->set(Fly_Constant::SESSION_COMBINED_AIR_PRICE_SOLUTIONS_PARAMETER, $combined_air_solutions_array);
    }

    public function getFlightSearchResult() {
        $session = new Session();
        return $session->get(Fly_Constant::SESSION_COMBINED_AIR_PRICE_SOLUTIONS_PARAMETER);
    }

    public function getSearchCriteria() {
        $session = new Session();
        return $session->get(Fly_Constant::SESSION_SEARCH_CRITERIA_PARAMETER);
    }

    public function searchNavDay($action = null) {
        if ($action != null) {
            $currentSearchCriteria = $this->getSearchCriteria();
            if ($action == "goprev") { // kalkış gununden 1 gun onceki
                $goDateTime = strtotime($currentSearchCriteria->godate);
                $goDateTime = strtotime('-1 day', $goDateTime);
                $currentSearchCriteria->godate = date("Y-m-d", $goDateTime);
            } else if ($action == "gonext") { // kalkısgununden 1 gun sonra
                $goDateTime = strtotime($currentSearchCriteria->godate);
                $goDateTime = strtotime('+1 day', $goDateTime);
                $currentSearchCriteria->godate = date("Y-m-d", $goDateTime);
            } else if ($action == "returnprev") { // donus gununden 1 gun once
                $returnDateTime = strtotime($currentSearchCriteria->returndate);
                $returnDateTime = strtotime("-1 day", $returnDateTime);
                $currentSearchCriteria->returndate = date("Y-m-d", $returnDateTime);
            } else if ($action == "returnnext") { // donus gununden 1 gun sonra
                $returnDateTime = strtotime($currentSearchCriteria->returndate);
                $returnDateTime = strtotime("+1 day", $returnDateTime);
                $currentSearchCriteria->returndate = date("Y-m-d", $returnDateTime);
            }
            return $this->search($currentSearchCriteria);
        }
        return null;
    }

    public function applyFilter($filterCriteriaObject = null) {

        if ($filterCriteriaObject == null) {
            return null;
        }

        $all_combined_air_price_solutions = $this->getFlightSearchResult();
        $filtered_combined_air_price_solutions = array();
        foreach ($all_combined_air_price_solutions as $combined_air_price_solution) {
            $filtered = true;
            $filtered_departure_journey_keys = array();
            $filtered_return_journey_keys = array();
            $is_all_departure_journeys_filtered = false;
            $is_all_return_journeys_filtered = false;

            //fiyat filtresi için 
            if (isset($filterCriteriaObject->{Fly_Constant::FILTER_MIN_PRICE_PARAMETER}) && isset($filterCriteriaObject->{Fly_Constant::FILTER_MAX_PRICE_PARAMETER})) {
                if ($filtered && !((int) $combined_air_price_solution->apprixomate_total_price_amount >= (int) $filterCriteriaObject->{Fly_Constant::FILTER_MIN_PRICE_PARAMETER} && (int) $combined_air_price_solution->apprixomate_total_price_amount <= (int) $filterCriteriaObject->{Fly_Constant::FILTER_MAX_PRICE_PARAMETER})) {
                    $filtered = false;
                }
            }
            // stopCount için 
            if ($filtered) {
                if (isset($filterCriteriaObject->{Fly_Constant::FILTER_IS_NO_STOP_PARAMETER}) || isset($filterCriteriaObject->{Fly_Constant::FILTER_ONE_STOP_PARAMETER}) || isset($filterCriteriaObject->{Fly_Constant::FILTER_TWO_MORE_STOP_PARAMETER})) {

                    $departure_journeys = $combined_air_price_solution->departure_journeys;
                    $return_journeys = $combined_air_price_solution->return_journeys;
                    $stop_count_filter_departure_journey_keys = array();
                    $stop_count_filter_return_journey_keys = array();

                    foreach ($departure_journeys as $departure_journey) {
                        $stopCount = $departure_journey->getStopCount();
                        $found_return_journey = null;
                        if ($return_journeys != null) {
                            foreach ($return_journeys as $return_journey) {
                                if ($return_journey->related_journey_id == $departure_journey->related_journey_id) {
                                    $found_return_journey = $return_journey;
                                    if ($stopCount < $return_journey->getStopCount()) {
                                        $stopCount = $return_journey->getStopCount();
                                        break;
                                    }
                                }
                            }
                        }
                        if (isset($filterCriteriaObject->{Fly_Constant::FILTER_IS_NO_STOP_PARAMETER}) && (int)$filterCriteriaObject->{Fly_Constant::FILTER_IS_NO_STOP_PARAMETER} == 1 ) {
                            if ($stopCount == 0) {
                                array_push($stop_count_filter_departure_journey_keys, $departure_journey);
                                if ($found_return_journey != null) {
                                    array_push($stop_count_filter_return_journey_keys, $found_return_journey);
                                }
                            }
                        } if (isset($filterCriteriaObject->{Fly_Constant::FILTER_ONE_STOP_PARAMETER}) && (int)$filterCriteriaObject->{Fly_Constant::FILTER_ONE_STOP_PARAMETER} == 1) {
                            if ($stopCount == 1) {
                                array_push($stop_count_filter_departure_journey_keys, $departure_journey);
                                if ($found_return_journey != null) {
                                    array_push($stop_count_filter_return_journey_keys, $found_return_journey);
                                }
                            }
                        }  if (isset($filterCriteriaObject->{Fly_Constant::FILTER_TWO_MORE_STOP_PARAMETER}) && (int)$filterCriteriaObject->{Fly_Constant::FILTER_TWO_MORE_STOP_PARAMETER} == 1) {
                            if ($stopCount > 1) {
                                array_push($stop_count_filter_departure_journey_keys, $departure_journey);
                                if ($found_return_journey != null) {
                                    array_push($stop_count_filter_return_journey_keys, $found_return_journey);
                                }
                            }
                        }
                    }
                      if(count($stop_count_filter_departure_journey_keys) < 1){
                          $filtered = false;
                      }
                      if($return_journeys != null && count($stop_count_filter_return_journey_keys) < 1){
                          $filtered  = false;
                      }
                    
                 }
                }
                if ($filtered) {
                    $departure_journeys = $combined_air_price_solution->departure_journeys;
                    $return_journeys = $combined_air_price_solution->return_journeys;

                    foreach ($departure_journeys as $departure_journey) {
                        $is_departure_journey_added = true;
                        if (isset($filterCriteriaObject->{Fly_Constant::FILTER_AIRLINE_COMPANY})) {
                            $airlineCompany = $filterCriteriaObject->{Fly_Constant::FILTER_AIRLINE_COMPANY};
                            if (!($departure_journey->getAirlineCompany() == $airlineCompany)) {
                                $is_departure_journey_added = false;
                            }
                        } else if(isset($filterCriteriaObject->{Fly_Constant::FILTER_SELECTED_AIRLINE_COMPANIES_PARAMETER})){
                           $selectedAirlineCompanies = $filterCriteriaObject->{Fly_Constant::FILTER_SELECTED_AIRLINE_COMPANIES_PARAMETER};
                           $airlineCompany = $departure_journey->getAirlineCompany();
                           if(!isset($selectedAirlineCompanies->{$airlineCompany})){
                               $is_departure_journey_added = false;
                           }
                         }
                        
                        if (isset($filterCriteriaObject->{Fly_Constant::FILTER_STOP_COUNT_PARAMTER})) {
                            $stopCount = (int) $filterCriteriaObject->{Fly_Constant::FILTER_STOP_COUNT_PARAMTER};
                            if (!($stopCount == $departure_journey->getStopCount())) {
                                $is_departure_journey_added = false;
                            }
                        }
                        
                        if(isset($filterCriteriaObject->{Fly_Constant::FILTER_GO_DEPARTURE_TIME_MINVALUE_PARAMETER}) && isset($filterCriteriaObject->{Fly_Constant::FILTER_GO_DEPARTURE_TIME_MAXVALUE_PARAMETER})){
                            $journey_departure_time = $departure_journey->getDepartureTime();
                            $total_minute = Fly_seach_helper::getDepartureTimeAsTotalMinute($journey_departure_time);
                            if(!((int)$filterCriteriaObject->{Fly_Constant::FILTER_GO_DEPARTURE_TIME_MINVALUE_PARAMETER} <= $total_minute && $total_minute <= (int)$filterCriteriaObject->{Fly_Constant::FILTER_GO_DEPARTURE_TIME_MAXVALUE_PARAMETER})){
                               $is_departure_journey_added = false;                       
                            }
                            
                        }
                        if(isset($filterCriteriaObject->{Fly_Constant::FILTER_GO_ARRIVAL_TIME_MAXVALUE_PARAMETER}) && isset($filterCriteriaObject->{Fly_Constant::FILTER_GO_ARRIVAL_TIME_MINVALUE_PARAMETER})){
                            $journey_arrival_time = $departure_journey->getArrivalTime();
                            $total_minute = Fly_seach_helper::getDepartureTimeAsTotalMinute($journey_arrival_time);
                            if(!((int)$filterCriteriaObject->{Fly_Constant::FILTER_GO_ARRIVAL_TIME_MINVALUE_PARAMETER} <= $total_minute && $total_minute <= (int)$filterCriteriaObject->{Fly_Constant::FILTER_GO_ARRIVAL_TIME_MAXVALUE_PARAMETER})){
                               $is_departure_journey_added = false;                       
                            }
                        }


                        if ($is_departure_journey_added) {
                            array_push($filtered_departure_journey_keys, $departure_journey->key);
                        }
                    }
                    if (!count($filtered_departure_journey_keys) > 0) {
                        $filtered = false;
                    }

                    if ($return_journeys != null) {
                        foreach ($return_journeys as $return_journey) {
                            $is_return_journey_added = true;
                            if (isset($filterCriteriaObject->{Fly_Constant::FILTER_AIRLINE_COMPANY})) {
                                $airlineCompany = $filterCriteriaObject->{Fly_Constant::FILTER_AIRLINE_COMPANY};
                                if (!($return_journey->getAirlineCompany() == $airlineCompany)) {
                                    $is_return_journey_added = false;
                                }
                            } else if(isset($filterCriteriaObject->{Fly_Constant::FILTER_SELECTED_AIRLINE_COMPANIES_PARAMETER})){
                               $selectedAirlineCompanies = $filterCriteriaObject->{Fly_Constant::FILTER_SELECTED_AIRLINE_COMPANIES_PARAMETER};
                               $airlineCompany = $return_journey->getAirlineCompany();
                               if(!isset($selectedAirlineCompanies->{$airlineCompany})){
                                $is_return_journey_added = false;
                               }
                         }
                            if (isset($filterCriteriaObject->{Fly_Constant::FILTER_STOP_COUNT_PARAMTER})) {
                                $stopCount = (int) $filterCriteriaObject->{Fly_Constant::FILTER_STOP_COUNT_PARAMTER};
                                if (!($stopCount == $return_journey->getStopCount())) {
                                    $is_return_journey_added = false;
                                }
                            }
                            
                            if(isset($filterCriteriaObject->{Fly_Constant::FILTER_RETURN_DEPARTURE_TIME_MINVALUE_PARAMETER}) && isset($filterCriteriaObject->{Fly_Constant::FILTER_RETURN_DEPARTURE_TIME_MAXVALUE_PARAMETER})){
                                 $journey_departure_time = $return_journey->getDepartureTime();
                                 $total_minute = Fly_seach_helper::getDepartureTimeAsTotalMinute($journey_departure_time);
                                 if(!((int)$filterCriteriaObject->{Fly_Constant::FILTER_RETURN_DEPARTURE_TIME_MINVALUE_PARAMETER} <=$total_minute && $total_minute <= (int)$filterCriteriaObject->{Fly_Constant::FILTER_RETURN_DEPARTURE_TIME_MAXVALUE_PARAMETER} )){
                                     $is_return_journey_added = false;
                                 }
                            }
                            
                            if(isset($filterCriteriaObject->{Fly_Constant::FILTER_RETURN_ARRIVAL_TIME_MAXVALUE_PARAMETER}) && isset($filterCriteriaObject->{Fly_Constant::FILTER_RETURN_ARRIVAL_TIME_MINVALUE_PARAMETER})){
                                 $journey_departure_time = $return_journey->getArrivalTime();
                                 $total_minute = Fly_seach_helper::getDepartureTimeAsTotalMinute($journey_departure_time);
                                 if(!((int)$filterCriteriaObject->{Fly_Constant::FILTER_RETURN_ARRIVAL_TIME_MINVALUE_PARAMETER} <=$total_minute && $total_minute <= (int)$filterCriteriaObject->{Fly_Constant::FILTER_RETURN_ARRIVAL_TIME_MAXVALUE_PARAMETER} )){
                                     $is_return_journey_added = false;
                                 }
                            }
                            

                            if ($is_return_journey_added) {
                                array_push($filtered_return_journey_keys, $return_journey->key);
                            }
                        }
                        if (!count($filtered_return_journey_keys) > 0) {
                            $filtered = false;
                        }
                    }
                }

                   
                if ($filtered == true) {
                    
                    $filtered_combined_air_price_object = new FilteredCombinedAirPriceSolution();
                    $filtered_combined_air_price_object->combined_key = $combined_air_price_solution->combined_key;
                    $filtered_combined_air_price_object->is_all_departures_journeys_filtered = $is_all_departure_journeys_filtered;
                    $filtered_combined_air_price_object->is_all_return_journeys_filtered = $is_all_return_journeys_filtered;
                    if(count($combined_air_price_solution->departure_journeys) == count($filtered_departure_journey_keys)){
                       $filtered_combined_air_price_object->is_all_departures_journeys_filtered = TRUE; 
                    }
                    if(count($combined_air_price_solution->return_journeys) == count($filtered_return_journey_keys)){
                        $filtered_combined_air_price_object->is_all_return_journeys_filtered = TRUE;
                    }
                    $filtered_combined_air_price_object->filtered_departure_journeys_keys = $filtered_departure_journey_keys;
                    $filtered_combined_air_price_object->filtered_return_journeys_keys = $filtered_return_journey_keys;
                    array_push($filtered_combined_air_price_solutions, $filtered_combined_air_price_object);
                }
            }
            return $filtered_combined_air_price_solutions;
        }
    }

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
?>
